package main

import (
	"fmt"
	"math/rand"
	"time"
)

//slice里是map
func sliceMap() {
	rand.Seed(time.Now().UnixNano())

	var s []map[string]int
	s = make([]map[string]int, 5, 16)
	for i, v := range s {
		fmt.Printf("slice[%d]=%v\n", i, v)
		/*
		   slice[0]=map[]
		   slice[1]=map[]
		   slice[2]=map[]
		   slice[3]=map[]
		   slice[4]=map[]
		*/
	}

	//s[0]["100"] = 100 //panic: assignment to entry in nil map

	s[0] = make(map[string]int, 16)
	s[0]["stu01"] = 1000
	s[0]["stu02"] = 2000
	s[0]["stu03"] = 3000

	for i, v := range s {
		fmt.Printf("slice[%d]=%v\n", i, v)
		/*
		   slice[0]=map[stu01:1000 stu02:2000 stu03:3000]
		   slice[1]=map[]
		   slice[2]=map[]
		   slice[3]=map[]
		   slice[4]=map[]
		*/
	}
}

//map里是slice
func mapSlice() {
	//var s map[string][]int
	s := make(map[string][]int, 16)

	key := "stu01"
	value, ok := s[key]
	if !ok {
		s[key] = make([]int, 0, 16)
		fmt.Printf("s[key]=%v addr:%p len:%d cap:%d\n", s[key], s[key], len(s[key]), cap(s[key])) //s[key]=[] addr:0xc0000d2080 len:0 cap:16
		value = s[key]
		fmt.Printf("value=%v addr:%p len:%d cap:%d\n", value, value, len(value), cap(value)) //value=[] addr:0xc0000d2080 len:0 cap:16
	}

	value = append(value, 100)
	fmt.Println("-------------------------------")
	fmt.Printf("value=%v addr:%p len:%d cap:%d\n", value, value, len(value), cap(value)) //value=[100] addr:0xc0000d2080 len:1 cap:16
	//todo ↓ 这里没有往s[key]追加进 y？
	fmt.Printf("s[key]=%v addr:%p len:%d cap:%d\n", s[key], s[key], len(s[key]), cap(s[key])) //s[key]=[] addr:0xc0000d2080 len:0 cap:16
	fmt.Println("-------------------------------")
	value = append(value, 200)
	fmt.Printf("value=%v addr:%p len:%d cap:%d\n", value, value, len(value), cap(value)) //value=[100 200] addr:0xc0000d2080 len:2 cap:16
	value = append(value, 300)
	fmt.Printf("value=%v addr:%p len:%d cap:%d\n", value, value, len(value), cap(value)) //value=[100 200 300] addr:0xc0000d2080 len:3 cap:16

	//s[key] = value /*← todo 这里必须添加这一句 y？*/

	fmt.Printf("map:%v\n", s) //map:map[stu01:[]] //map:map[stu01:[100 200 300]]
}

func mapSlice2() {
	m := make(map[string][]int, 16)
	key := "stu01"

	if _, ok := m[key]; !ok {
		m[key] = make([]int, 0, 16)
	}

	m[key] = append(m[key], 100)
	m[key] = append(m[key], 200)
	m[key] = append(m[key], 300)

	fmt.Println(m) //map[stu01:[100 200 300]]
}

func test() {
	x := make([]int, 0, 16)
	fmt.Printf("x=%v addr:%p len:%d cap:%d\n", x, x, len(x), cap(x)) //x=[] addr:0xc00007a080 len:0 cap:16
	x2 := x
	fmt.Printf("x2=%v addr:%p len:%d cap:%d\n", x2, x2, len(x2), cap(x2)) //x2=[] addr:0xc00007a080 len:0 cap:16
	x3 := x
	fmt.Printf("x3=%v addr:%p len:%d cap:%d\n", x3, x3, len(x3), cap(x3)) //x3=[] addr:0xc00007a080 len:0 cap:16

	x2 = append(x2, 100, 200, 300)
	fmt.Printf("x2=%v addr:%p len:%d cap:%d\n", x2, x2, len(x2), cap(x2)) //x2=[100 200 300] addr:0xc00007a080 len:3 cap:16
	x3 = append(x3, 200)
	fmt.Printf("x3=%v addr:%p len:%d cap:%d\n", x3, x3, len(x3), cap(x3)) //x3=[200] addr:0xc00007a080 len:1 cap:16

	fmt.Printf("x=%v addr:%p len:%d cap:%d\n", x, x, len(x), cap(x)) //x=[] addr:0xc00007a080 len:0 cap:16

	fmt.Println(x, x2, x3) //[] [200 200 300] [200] todo y? 不是引用类型吗 不应该都一样吗
}

func main() {
	//sliceMap()

	//mapSlice()

	//mapSlice2()

	test()
}
